<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        list-style: none;
        margin: 0;
        padding: 0;
      }

      #shop-car li {
        border: 1px dotted red;
        margin: 20px 0;
        padding: 20px;
      }

      .container {
        width: 400px;
        padding: 20px;
      }
    </style>
  </head>

  <body>
    <div class="container">
      <input type="checkbox" id="isAllCheck"  /> 全选

      <ul id="shop-car">
        <!-- <li>
          <input type="checkbox" />
          <div>商品名称:</div>
          <img
            src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg"
            alt=""
          />
          <div>商品价格: <span>50</span></div>
          <div>购买数量:30</div>
          <div>商品规格: 黑</div>
          <div>是否选中：选中吧OR不选中</div>
        </li> -->
      </ul>
    </div>
    <script>
      let products = [
        {
          goods_name: '小米10-1',
          goods_img:
            'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
          goods_price: '500',
          goods_attr: '砂石黑4GB+64GB',
          goods_num: 10,
          goods_is_checked: true,
        },
        {
          goods_name: '小米10-2',
          goods_img:
            'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
          goods_price: '510',
          goods_attr: '砂石黑4GB+64GB',
          goods_num: 50,
          goods_is_checked: false,
        },
      ];
      let str = '';
      let num = 0;
      let price = 0;
      let isAllCheck = document.querySelector('#isAllCheck');
      let ul = document.querySelector('#shop-car');
      products.forEach(function (item, index) {
        str += `<li>
          <div>商品名称:${item.goods_name}</div>
          <img
            src=${item.goods_img}
            alt=""
          />
          <div>商品价格:${item.goods_price}</div>
          <div>购买数量:${item.goods_num}</div>
          <div>商品规格: ${item.goods_attr}</div>
          <div>是否选中：<input type="checkbox"${
            item.goods_is_checked ? 'checked' : ''
          } /></div>
        </li>`;
      });
      let tag = products.every(function (item, index) {
        return item.goods_is_checked;
      });
      for (let i = 0; i < products.length; i++) {
        if (tag) {
          num += products[i].goods_num;
          isAllCheck.setAttribute('checked',true);
          price += (products[i].goods_num)*Number(products[i].goods_price);
        } else {
            isAllCheck.removeAttribute('checked');
            if(products[i].goods_is_checked){
                num+=products[i].goods_num;
                price = (products[i].goods_num)*Number(products[i].goods_price);
            }
        }
      }
      str += `<p id="totalnum">总数量:${num}</p>
        <p id="totalprice">总价格:${price}</p>`;
      ul.innerHTML += str;
    </script>
  </body>
</html>
